/**
 * 
 */
package com.handinfo.uimenu;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.uimenu.AbsHyperLinker;
import com.handinfo.uimenu.UIAbstractContext;
import com.handinfo.uimenu.UITools;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

/**
 * @author venizeng
 * 
 */
public class UIMessageBox extends UIAbstractContext
{
	private final String TITLE_DEFAULT = "提示";	
	//
	private final int H_TITLE = UIManager.IMAGE_TITlE_MIDDLE.getHeight();
	private final int IH_TITLE_CONTENT = UITools.IH_RECT_CONTENT;
	private final int IH_CONTENT_BOTTON = UITools.IH_RECT_CONTENT;
	//
	private final int W_BORDER_RECT = 8;
	private final int W_RECT_CONTENT = 4;
	//
	private final int X_CONTENT = W_BORDER_RECT + W_RECT_CONTENT;
	private final int Y_CONTENT = H_TITLE + IH_TITLE_CONTENT + W_RECT_CONTENT;
	//
	private final int H_PRE_CONTENT = AbstractGameScreen.FONT_H;//
	//
	private String TITLE = null;
	private String CONTENT = null;
	private String LB = null;
	private String RB = null;
	private int ANCHOR = Tools.LEFT_TOP;// 内容层的对其方式
	private int COLOR = UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS);
	//
	private final int W_RECT;
	private final int W_CONTENT;
	//
	private int m_h_rect;
	private int m_h_content;

	protected UIMessageBox(int id)
	{
		super(id);
		m_w = UITools.W_BG;
		W_RECT = m_w - (W_BORDER_RECT << 1);
		W_CONTENT = W_RECT - (W_RECT_CONTENT << 1);
	}
	
	public void showNotify()
	{
		super.showNotify();
		if (m_focusable) {
			// 站立
			if (GameManager.getInstance().m_role != null) {// 登陆时候
				GameManager.getInstance().m_role.stand();
			}
		}
	}
	
	
	
	protected void registerTouch() {
		registerNormalSoft();
	}

	public void setMessage(String content)
	{
		setMessage(TITLE_DEFAULT, content);
	}
	
	public void setMessage(String title, String content)
	{
		setMessage(title, content, "确定", "返回",ANCHOR_LT);
	}
	
	public void setMessage(String title, String lb, String rb, String content)
	{
		setMessage(title, content, lb, rb,ANCHOR_LT);
	}

	/**
	 * 设置信息
	 * 2012-2-27 下午07:14:28
	 * venizeng
	 * @param title
	 * @param content
	 * @param lb
	 * @param rb
	 * @param anchor_str
	 */
	public void setMessage(String title, String content, String lb, String rb, int anchor_str)
	{
		TITLE = title == null ? TITLE_DEFAULT : title;
		CONTENT = content == null ? "" : content;
		LB = lb;
		RB = rb;
		ANCHOR = anchor_str;
		//
		setContent(CONTENT);
		m_h_rect = m_h_content + (W_RECT_CONTENT << 1) ;
		//
		m_h = H_TITLE + IH_TITLE_CONTENT + m_h_rect + IH_CONTENT_BOTTON ;
		if (LB != null || RB != null) {
			m_h +=  UITools.H_LABEL;// H_BUTTON = UITools.H_LABEL + UITools.IH_IMAGE_SOFT;
		}
		setCentre();
	}

	/**
	 * 设置内容层高度 2012-2-27 下午05:33:35 venizeng
	 * @param content
	 */
	private void setContent(String content)
	{
		if (AbsHyperLinker.isHyperLink(content)) {
			AbsHyperLinker hyperLinker = AbsHyperLinker.createHyperLinker(content, W_CONTENT, 200);
			m_h_content = hyperLinker.getRealHeight();
			hyperLinker.setPreH(H_PRE_CONTENT);
			hyperLinker.setH(m_h_content);
			m_type = TYPE_STR_HYPER;
			m_data = hyperLinker;
		}
		else {
			String[] strs = Tools.getStrArray(content, AbstractGameScreen.s_font, W_CONTENT);
			m_h_content = strs.length * H_PRE_CONTENT;
			m_type = TYPE_STR_NORS;
			m_data = strs;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.handinfo.uimenu.UIAbstractWindow#paintContent(javax.microedition.lcdui.Graphics)
	 */
	public void paintContent(Graphics g)
	{
		UITools.drawAlphaBG(g);
		// 绘制背景
		UITools.drawWindowBackgroud(g, m_x, m_y, m_w, m_h);
		// 绘制标题
		UITools.paintBackgroundTitleOn240320(g, m_x, m_y, m_w, m_h, TITLE);
//	//	绘制按钮
//		UITools.paintBackgroundButton(g, m_x, m_y, m_w, m_h, LB != null ,RB != null);
		//
		paintContents(g);
		UITools.paintBorderConer(g, m_x, m_y, m_w, m_h);
		//	绘制按钮
		UITools.paintBackgroundButton(g, m_x, m_y, m_w, m_h, LB != null ,RB != null);
	}

	public AbsHyperLinker getHyperLinker(){
		if (m_data == null) {
			return null ;
		}
		if (m_data instanceof AbsHyperLinker) {
			return (AbsHyperLinker) m_data ;
		}
		return null ;
	}
	
	private void paintContents(Graphics g)
	{
		int initX = m_x + X_CONTENT;
		int initY = m_y + Y_CONTENT;
		//
		Object data = m_data;
		int type = m_type;
		int color = COLOR;
		if (data == null) {
			return;
		}
		switch (type) {
			case TYPE_STR_NOR: {
				String str = (String) data;
				g.setColor(color);
				drawAnchorStr(g, str, initX, initY, W_CONTENT, H_PRE_CONTENT, ANCHOR) ;
			}
				break;
			case TYPE_STR_NORS: {
				String[] strs = (String[]) data;
				g.setColor(color);
				int tempY = initY;
				for (int i = 0; i < strs.length; i++) {
					if (strs[i] == null) {
						continue;
					}
					drawAnchorStr(g, strs[i], initX, tempY, W_CONTENT, H_PRE_CONTENT, ANCHOR) ;
					tempY += H_PRE_CONTENT;
				}
			}
				break;
			case TYPE_STR_HYPER: {
				AbsHyperLinker hyperLinker = (AbsHyperLinker) data;
				hyperLinker.paintHyperLink(g, initX, initY);
			}
				break;
			default:
				throw new IllegalArgumentException("无此类型  type = " + m_type);
		}
	}

	private void drawAnchorStr(Graphics g, String str,int x, int y,int w,int h,int anchor)
	{
		if (str == null) {
			return ;
		}
		int tempY = y + ((h - AbstractGameScreen.FONT_H) >> 1) ;
		switch (anchor) {
			case ANCHOR_LT:
				g.drawString(str, x, tempY, 0);
				break;
			case ANCHOR_RT:
				g.drawString(str, x + w, tempY, Tools.RIGHT_TOP);
				break;
			case ANCHOR_HT:
				g.drawString(str, x + ((w - AbstractGameScreen.s_font.stringWidth(str)) >> 1), tempY, Tools.RIGHT_TOP);
				break;
			default:
				break;
		}
	}
	
	

}
